package com.Maybe.book.controller;

import com.Maybe.book.constants.Constants;
import com.Maybe.book.model.UserInfo;
import com.Maybe.book.service.UserService;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping(value = "/login",produces = "application/json")
    public Boolean login(String name, String password, HttpSession session){
        //1.参数校验
        if(!StringUtils.hasLength(name)||!StringUtils.hasLength(password)){
            return false;
        }
        UserInfo login = userService.login(name);
        if(login==null){
            return false;
        }
        if(login!=null&&password.equals(login.getPassword())){
            //先清空密码，再存入session，为了安全性
            login.setPassword("");
            session.setAttribute(Constants.SESSION_USER_KEY,login);
            return true;
        }
        return false;




        //2.校验帐号与密码是否正确
//        if("admin".equals(name)&&"admin".equals(password)){
//            //3.设置session
//            session.setAttribute("userName",name);
//            return true;
//        }
//        return false;
    }

}
